package com.sky.service.impl;

import com.sky.entity.Orders;
import com.sky.mapper.DishMapper;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.WorkspaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @Description TODO
 * @Author dingchunlin
 * @Date 2023/8/27 12:53
 * @Version 1.0.0
 */
@Service
@Slf4j
public class WorkspaceServiceImpl implements WorkspaceService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private OrderMapper orderMapper;

    @Override
    public SetmealOverViewVO overviewSetmeals() {
        return setmealMapper.getNumByStatus();
    }

    @Override
    public DishOverViewVO overviewDishes() {
        return dishMapper.getNumByStatus();
    }

    @Override
    public BusinessDataVO businessData() {
        //创建一个返回类
        BusinessDataVO businessDataVO = new BusinessDataVO();
        //获取当前时间
        LocalDateTime now = LocalDateTime.now();
        //将时间转成年月日形式
        String time = now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        //赋值新增用户
        businessDataVO.setNewUsers(Integer.parseInt(userMapper.getAddCount(time)));
        //设置当天营业额
        Integer sumAmount = orderMapper.sumAmount(time, Orders.COMPLETED);

        //当天订单完成数
        Integer validOrderCount = orderMapper.validOrderCount(time, Orders.COMPLETED);
        //查询当天的全部订单
        Integer allCount = orderMapper.validOrderCount(time, null);
        //当天有效订单
        businessDataVO.setValidOrderCount(validOrderCount);
        //设置订单完成率
        businessDataVO.setOrderCompletionRate(validOrderCount / (allCount*1.0));
        //设置平均客单价
        if (sumAmount != null) {
            businessDataVO.setTurnover(Double.valueOf(sumAmount));
            businessDataVO.setUnitPrice(Double.valueOf(sumAmount)/allCount);
        }
        return businessDataVO;
    }

    @Override
    public OrderOverViewVO overviewOrders() {
        OrderOverViewVO orderOverViewVO = new OrderOverViewVO();
        //获取当前时间
        LocalDateTime now = LocalDateTime.now();
        //将时间转成年月日形式
        String time = now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        orderOverViewVO.setAllOrders(orderMapper.validOrderCount(time,null));
        orderOverViewVO.setCancelledOrders(orderMapper.validOrderCount(time,Orders.CANCELLED));
        orderOverViewVO.setCompletedOrders(orderMapper.validOrderCount(time,Orders.COMPLETED));
        orderOverViewVO.setDeliveredOrders(orderMapper.validOrderCount(time,Orders.DELIVERY_IN_PROGRESS));
        orderOverViewVO.setWaitingOrders(orderMapper.validOrderCount(time,Orders.TO_BE_CONFIRMED));
        return orderOverViewVO;
    }
}
